package com.topisv.tms.service.report;

import java.io.OutputStream;
import java.util.ArrayList;
import java.util.Date;
import java.util.LinkedHashMap;
import java.util.List;

import javax.servlet.http.HttpServletResponse;

import org.apache.commons.lang3.time.DateFormatUtils;
import org.springframework.stereotype.Service;

import com.topisv.tms.mybatis.Pagination;
import com.topisv.tms.mybatis.service.CommonService;
import com.topisv.tms.tools.ExcelStUtil;
//import com.topisv.tms.vo.report.InstorageReportVo;
import com.topisv.tms.vo.report.KpiReportVo;

/**
 * KPI收货报表
 *
 * @author yuansheng
 * @version 1.0.0
 */
@Service
public class KpiReceiveService extends CommonService {

    public Pagination getPageKpiReceive(KpiReportVo vo) throws Exception {
        Pagination page = myBatisPage("kpiReceiveMapper.getCount", "kpiReceiveMapper.getList", vo);
        return page;
    }

    public List<Object> getOPName() {
        return sqlMapDao.getSqlSession().selectList("kpiReceiveMapper.getOPName");
    }

    public void exportExcel(KpiReportVo vo, HttpServletResponse response) throws Exception {
        OutputStream output = response.getOutputStream();
        List rglist = sqlMapDao.selectList("kpiReceiveMapper.getList", vo);
        List<Object[]> list = new ArrayList<Object[]>();
        for (Object object : rglist) {
            LinkedHashMap<String, String> map = (LinkedHashMap<String, String>) object;
            Object[] obj = new Object[19];
            obj[0] = map.get("id");
            obj[1] = map.get("入库单号");
            obj[2] = map.get("单据类型");
            obj[3] = map.get("状态");
            obj[4] = map.get("仓库名称");
            obj[5] = map.get("客户名称");
            obj[6] = map.get("SKU数量");
            obj[7] = map.get("总数量");
            obj[8] = map.get("上架SKU数量");
            obj[9] = map.get("上架数量");
            obj[10] = map.get("登记时间");
            obj[11] = map.get("点收日期");
            obj[12] = map.get("开始点收时间");
            obj[13] = map.get("点收结束时");
            obj[14] = map.get("点收操作人");
            obj[15] = map.get("上架任务创建时间");
            obj[16] = map.get("上架任务完成时间");
            obj[17] = map.get("上架任务完成时间");
            obj[18] = map.get("创建日期");
            list.add(obj);
        }
        
        response.setContentType("application/octet-stream");// 设置后会呼出下载界面
        // 头信息
        String[] titles = { "数据库序号", "入库单号", "单据类型", "状态", "仓库名称", "客户名称", "SKU数量", "总数量", "上架SKU数量", "上架数量", "登记时间", "点收日期", "开始点收时间", "点收结束时间", "点收操作人",
                "上架任务创建时间", "上架任务完成时间", "上架操作人", "创建日期" };
        // 文件名
        String fileName = new String(("KPI收货报表.xls").getBytes("GBK"), "ISO8859-1");
        response.setHeader("Content-disposition", "attachment; filename=" + fileName);
        String pageTitle = "KPI收货报表";
        Date now = new Date();
        ExcelStUtil.export(output, DateFormatUtils.format(now, "yyyyMMddhhmmss"), pageTitle, titles, list);
        output.close();
    }
}
